package com.hula.onion.book.server.controller;

import com.hula.onion.book.server.service.AutoSyncService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Description:
 * Create Date:2018/1/18 16:42
 *
 * @author liangbing
 * @version 1.0
 */
@RestController
@RequestMapping("/onion-book/api/v1")
@Api(value = "数据自动同步API")
public class AutoSyncController {

    @Autowired
    private AutoSyncService autoSyncService;

    @ApiOperation(value = "自动同步")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "uname", value = "用户名", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "pwd", value = "密码", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "ticket", value = "验证码", dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "platformId", value = "p2p平台编号", dataType = "int", paramType = "form", required = true),
            @ApiImplicitParam(name = "userId", value = "user 唯一标识", dataType = "String", paramType = "form", required = true)
    })
    @RequestMapping(value = "/autosync", method = RequestMethod.POST)
    public String autoSync(@RequestParam("uname") String userName,
                           @RequestParam("pwd") String password,
                           @RequestParam(value = "ticket", required = false) String ticket,
                           @RequestParam("platformId") int platformId,
                           @RequestParam("userId") String userId,
                           HttpServletRequest request,
                           HttpServletResponse response) {
        return autoSyncService.doAutoSync(userName, password, ticket, platformId, userId, request, response);
    }

    @ApiOperation(value = "手动同步")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "platformId", value = "p2p平台编号", dataType = "int", paramType = "form", required = true),
            @ApiImplicitParam(name = "userId", value = "user id", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "amount", value = "账户余额", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "totalIncome", value = "总收益", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "waitIncome", value = "待收收益", dataType = "String", paramType = "form", required = true)
    })
    @RequestMapping(value = "/manualsync", method = RequestMethod.POST)
    public String manualSync(@RequestParam("platformId") int platformId,
                             @RequestParam("userId") String userId,
                             @RequestParam("amount") String amount,
                             @RequestParam("totalIncome") String totalIncome,
                             @RequestParam("waitIncome") String waitIncome) {
        return autoSyncService.doManualSync(platformId, userId, amount, totalIncome, waitIncome);
    }

    @ApiOperation(value = "获取验证码")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "platformId", value = "p2p平台编号", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "userFlag", value = "user 唯一标识", dataType = "String", paramType = "query", required = true)
    })
    @RequestMapping(value = "/captcha", method = RequestMethod.GET)
    public void getCaptcha(@RequestParam("platformId") int platformId,
                           @RequestParam("userFlag") String userFlag,
                           HttpServletResponse response) throws Exception {
        autoSyncService.getCaptcha(platformId, userFlag, response);
    }

    @ApiOperation(value = "删除绑定的p2p平台信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "绑定平台信息主键id", dataType = "int", paramType = "query", required = true)
    })
    @RequestMapping(value = "/delSyncInfo", method = RequestMethod.POST)
    public String getCaptcha(@RequestParam("id") int id) throws Exception {
        return autoSyncService.doDelSyncInfo(id);
    }

}
